// packageB/member/category/component/goodScreen/goodScreen.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean
    },
    showCategory: {
      type: Boolean
    },
    yzSupplyLease:{
      type:Boolean,
      value:false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    goodScreenAll: [],
    screentitle: ["品牌", "分类", "属性", "高级选项"], //筛选标题
    showPop: false,
    show1: true,
    show2: false,
    show3: false,
    show4: false,
    select: "",
    brand: [],
    attr: [{
        name: "推荐商品",
        rot: "is_recommand",
        show: true
      },

      {
        name: "新品上市",
        rot: "is_new",
        show: true
      },

      {
        name: "热卖商品",
        rot: "is_hot",
        show: true
      },

      {
        name: "促销商品",
        rot: "is_discount",
        show: true
      },

      {
        name: "限时抢购",
        rot: "limit_buy",
        show: true
      }
    ],
    attrstr: [],
    attrkey: [],
    classification1: [],
    classification2: [],
    classificationId: null,
    classificationId1: null,
    classificationId2: null,
    classificationParentIndex: 0,

    tagname: [],
    showtagnamekey: null,
    typestring: "", //搜索分类id

    filterId: [],
    brandId: '',
    classShowind:'',
    classShowName:'',
    top:'',
    mini:'',
    supplierInfo:[]
  },
  observers: {
    'show': function (val) {
      this.setData({
        showPop: val
      });
      if (val) {
        if (this.data.brand.length == 0) this.getbrand();
        if (this.data.classification1.length == 0) this.getclassification();
        if (this.data.tagname.length == 0) this.getname();
        if (this.data.yzSupplyLease && this.data.supplierInfo.length == 0) this.getSupplier()
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //品牌相关的功能
    getbrand() {
      let _url = app.getNetAddresss("goods.brand.get-brand")
      app._postNetWork({
        url: _url,
        showToastIn: false,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.setData({
              brand: res.data.data
            });
          }
        }
      });
    },
    //分类相关的功能
    getclassification() {
      app._getNetWork({
        showToastIn: false,
        url: app.getNetAddresss("goods.category.search-goods-category"),
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.setData({
              classification1: res.data
            });
          }
        }
      });
    },
    getSupplier(){
      app._getNetWork({
        url: app.getNetAddresss("plugin.yz-supply-lease.api.goods.supplier-list"),
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.setData({
              supplierInfo: res.data
            });
          }
        }
      });
    },
    getname() {
      app._getNetWork({
        showToastIn: false,
        url: app.getNetAddresss("goods.filtering.index"),
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            let tagname = res.data;
            for (let i = 0; i < tagname.length; i++) {
              for (let y = 0; y < tagname[i].value.length; y++) {
                if (this.data.filterId.includes(tagname[i].value[y].id)) {
                  tagname[i].value[y].record = 2;
                } else {
                  tagname[i].value[y].record = 1;
                }

              }
            }
            this.setData({
              tagname: tagname
            });
          }
        }
      });
    },
    onClose() {
      this.setData({
        showPop: false
      });
      this.triggerEvent('clone');
    },
    onChangeGoodScreenAll(event) {
      this.setData({
        goodScreenAll: event.detail
      });
    },
    inpTop(e){
      console.log(e,'ddddddddddd')
      this.setData({
        top:e.detail
      })
    },
    inpMini(e){
      this.setData({
        mini:e.detail
      })
    },
    onChange(event) {
      this.setData({
        activeName: event.detail
      });
    },
    changetype(e) {
      let key = e.currentTarget.dataset.key;
      this.setData({
        select: key
      });
      this.resetselect();
      switch (key) {
        case 0:
          this.setData({
            show1: true
          });
          break;
        case 1:
          this.setData({
            show2: true
          });
          break;
        case 2:
          this.setData({
            show3: true
          });
          break;
        case 3:
          this.setData({
            show4: true
          });
          break;
        default:
          break;
      }
    },
    resetselect() {
      this.setData({
        show1: false,
        show2: false,
        show3: false,
        show4: false
      });
    },
    screenbrand(e) {
      let id = e.currentTarget.dataset.id;
      this.setData({
        brandId: id,
      });
      this.cloneModel();
    },
    screensupplier(e) {
      let id = e.currentTarget.dataset.id;
      this.setData({
        supplier_id: id,
      });
      this.cloneModel();
    },
    classificationtype(e) {
      let id = e.currentTarget.dataset.id;
      // let key = e.currentTarget.dataset.key;
      this.setData({
        classification2: [],
        typestring: id
      });
      if (this.data.classificationId == id) {
        this.setData({
          classificationId: null
        });
        return;
      } else {
        this.setData({
          classificationId: id
        });
      }
      this.setData({
        classificationId1: null,
        classificationId2: null,
      });
    },
    classificationtype1(e) {
      let id = e.currentTarget.dataset.id;
      let ind = e.currentTarget.dataset.key;
      let val = e.currentTarget.dataset.val;
      let parentIndex = e.currentTarget.dataset.parentIndex;

      this.setData({
        typestring: id,
        classificationParentIndex: parentIndex,
      });
      if (this.data.classificationId1 == id) {
        this.setData({
          classificationId1: null,
          classShowName:'',
          classShowind:''
        });
      } else {
        this.setData({
          classificationId1: id,
          classShowName:val.name,
          classShowind:ind
        });
      }

      this.setData({
        classificationId2: null
      });
    },
    classificationtype2(e) {
      let id = e.currentTarget.dataset.id;
      let ind = e.currentTarget.dataset.key;
      let val = e.currentTarget.dataset.val;
      if (this.data.typestring == id) {
        this.setData({
          typestring: null
        });
      } else {
        this.setData({
          typestring: id
        });
      }

      if (this.data.classificationId2 == id) {
        this.setData({
          classificationId2: null,
          classShowName:'',
          classShowind:''
        });
      } else {
        this.setData({
          classificationId2: id,
          classShowName:val.name,
          classShowind:ind
        });
      }
      console.log('ddddddddd',this.data.classShowName,this.data.classShowind)
    },
    selectattr(e) {
      let key = e.currentTarget.dataset.key;
      console.log(key);
      //属性选择项
      switch (key) {
        case 0:
          this.findstr("is_recommand", key);
          break;
        case 1:
          this.findstr("is_new", key);
          break;
        case 2:
          this.findstr("is_hot", key);
          break;
        case 3:
          this.findstr("is_discount", key);
          break;
        case 4:
          this.findstr("limit_buy", key);
          break;
      }
      if (this.data.attr[key].show) {
        this.setData({
          ['attr[' + key + '].show']: false
        });
      } else {
        this.setData({
          ['attr[' + key + '].show']: true
        });
      }
    },
    //属性相关的功能
    findstr(attrstr, key) {
      let index = this.data.attrstr.indexOf(attrstr);
      if (index + 1) {
        let attrstrD = this.data.attrstr;
        attrstrD.splice(index, 1);
        this.setData({
          attrstr: attrstrD
        });
      } else {
        let attrstrD = this.data.attrstr;
        attrstrD.push(attrstr);
        this.setData({
          attrstr: attrstrD
        });
      }

      let index1 = this.data.attrkey.indexOf(key);
      if (index1 + 1) {
        let attrkey = this.data.attrkey;
        attrkey.splice(index1, 1);
        this.setData({
          attrkey: attrkey
        });
      } else {
        let attrkey = this.data.attrkey;
        attrkey.push(key);
        this.setData({
          attrkey: attrkey
        });
      }
    },
    //标签组相关功能
    showtagname(e) {
      let key = e.currentTarget.dataset.key;
      if (key == this.data.showtagnamekey) {
        this.setData({
          showtagnamekey: null
        });
        return;
      } else {
        this.setData({
          showtagnamekey: key
        });
      }
    },
    selectname(e) {
      let id = e.currentTarget.dataset.id;
      let key1 = e.currentTarget.dataset.key;
      let key2 = e.currentTarget.dataset.keyx;

      if (this.data.tagname[key1].value[key2].record == 1) {
        if (this.data.filterId.includes(id)) {
          return;
        } else {
          let filterId = this.data.filterId;
          filterId.push(id);
          this.setData({
            filterId: filterId
          });
        }
        this.setData({
          ['tagname.[' + key1 + '].value[' + key2 + '].record']: 2
        });
      } else {
        let index = this.data.filterId.indexOf(id);
        let filterId = this.data.filterId;
        filterId.splice(index, 1);
        this.setData({
          filterId: filterId
        });
        this.setData({
          ['tagname.[' + key1 + '].value[' + key2 + '].record']: 1
        });
      }
    },
    resetnames() {
      this.setData({
        showtagnamekey: null,
        brandId: "",
        attrstr: [],
        filterId: [],
        typestring: "",
        attr: [{
            name: "推荐商品",
            show: true
          },

          {
            name: "新品上市",
            show: true
          },

          {
            name: "热卖商品",
            show: true
          },

          {
            name: "促销商品",
            show: true
          },

          {
            name: "限时抢购",
            show: true
          }
        ],
        classificationkey: null,
        classificationkey1: null,
        classificationkey2: null
      });
      for (let i = 0; i < this.data.tagname.length; i++) {
        for (let y = 0; y < this.data.tagname[i].value.length; y++) {
          if (this.data.tagname[i].value[y].record == 2) {
            this.setData({
              ['tagname.[' + i + '].value[' + y + '].record']: 1
            });
          }
        }
      }
      this.cloneModel();
    },
    screen() {
      this.cloneModel();
    },
    cloneModel() {
      let data = {
        brandId: this.data.brandId,
        attrstr: this.data.attrstr.toString(),
        filterId: this.data.filterId.toString(),
        typestring: this.data.typestring,
        top:this.data.top,
        mini:this.data.mini,
      }
      if(this.data.yzSupplyLease){
        data.supplier_id = this.data.supplier_id;
      }
      this.triggerEvent('confirm', data);
      this.onClose();
    }
  }
})